


//constructor 
function ChatClient(variable){
    this.windows = new Array();
    this.msgTimer="";
    this.membersTimer="";
    this.referenceExcl=variable;
    this.receiver="";
    this.startTimers();
}

ChatClient.prototype.startTimers=function()
{
	this.msgTimer = window.setInterval(this.referenceExcl+".updateUser()",3000);
	this.membersTimer = window.setInterval(this.referenceExcl+".updateMembers()",10000);
} 



ChatClient.prototype.stopTimers=function()
{
    window.clearInterval (this.msgTimer);
    window.clearInterval (this.membersTimer);
    this.msgTimer="";
    this.membersTimer="";
}
ChatClient.prototype.updateUser=function()
{
    PageMethods.UpdateUser($get("hdnRoomID").value, this.UpdateMessages);
}
ChatClient.prototype.updateMembers=function()
{
    PageMethods.UpdateRoomMembers($get("hdnRoomID").value, this.UpdateMembersList);
}
ChatClient.prototype.Leave=function ()
{
    this.stopTimers();
    PageMethods.LeaveRoom($get("hdnRoomID").value);
}

ChatClient.prototype.button_clicked=function ()
{
	PageMethods.SendMessage($get("txtMsg").value,$get("hdnRoomID").value,this.UpdateMessages, this.errorCallback);
	$get("txtMsg").value="";
	
}
ChatClient.prototype.clickButton=function(e, buttonid)
{ 
	var bt = document.getElementById(buttonid); 
	if (typeof bt == 'object')
		{ 
			if(navigator.appName.indexOf("Netscape")>(-1))
			{ 
			    if (e.keyCode == 13)
			    { 
				    bt.click(); 
				    return false; 
			    } 
		    } 
	    if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1))
		    { 
			    if (event.keyCode == 13)
			    { 
			        bt.click(); 
				    return false; 
			    } 
		    } 
	    }	 
} 

ChatClient.prototype.ParserMsgLine=function(msg){
    if (msg && msg.Msg!=""){
        var currentUser=$get("hdnCurrentAccount").value;
        var color=(currentUser==msg.Sender)?"black":"Blue";
        var str="<span style='color:{0}'><b style='font-size:13px;'>{1}:</b> {2}</span><br/>";        
        return StringUtils.format(str,color,msg.Sender,msg.Msg);    
    }else return "";
}

ChatClient.prototype.UpdateMessages=function (result)
{
    var display=$get("displayMessage");   
    if (result){        
      for (var i=0;i< result.length;i++){                
         display.innerHTML=display.innerHTML+chatClient.ParserMsgLine(result[i]); 
      }
    }
    display.scrollTop=display.scrollHeight-display.offsetHeight;
    
    //$get("txt").value=$get("txt").value+result;
    
}

ChatClient.prototype.UpdateMembersList=function(result)
{
   // alert(result);
    var users=result.split(",");
   // alert(users.length);
    var i=0;
    
    
    $get("lstMembers").options.length=0;
     var i=0;
    while (i < users.length)
    {
        if (users[i]!="");
        {
            var op=new Option(users[i],users[i]);
            $get("lstMembers").options[$get("lstMembers").options.length]= op;
        }
        i+=1;
    }
    
    //$get("lstMembers").value=$get("txt").value+result;
    //$get("txt").doScroll();
}
ChatClient.prototype.errorCallback=function(result)
{
	alert("An error occurred while invoking the remote method: " 
	+ result);
}



var chatClient=new ChatClient("chatClient");		